package com.gisuni.sddc.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.codehaus.jackson.map.ObjectMapper;
import org.json.JSONObject;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.GET;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.POST;
import org.nutz.mvc.annotation.Param;

import com.gisuni.sddc.model.Response;
import com.gisuni.sddc.service.ClusterService;
import com.gisuni.sddc.service.LandStatisticsService;

/**
 * 聚合分析
 *
 */
@IocBean(singleton = false)
@At("/client/service/clusterController") 
public class ClusterController {
	private Log log = Logs.getLog(ClusterService.class);
	@Inject
	private ClusterService  clusterService;
	
	/**
	 * 查询
	 * 
	 * @return
	 */
	@At("/clusterData")
	@GET
	@POST
	@Ok("json")
	public Object clusterData(@Param("clusterParam") String clusterParam ,HttpServletRequest request) {
		Response res = new Response();
		res.setSuccess(false);
		Object r = res;
		try {
			res = clusterService.clusterLandData(clusterParam ,request);
			JSONObject o = Response.toJSONObject(res);
			ObjectMapper mapper = new ObjectMapper();
			r = mapper.readValue(o.toString(), Map.class);
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return r;
	}
	
	/**
	 * 查询 具体信息
	 * 
	 * @return
	 */
	@At("/clusterDetailData")
	@GET
	@POST
	@Ok("json")
	public Object clusterDetailData(@Param("clusterParam") String clusterParam ,HttpServletRequest request) {
		Response res = new Response();
		res.setSuccess(false);
		Object r = res;
		try {
			res = clusterService.clusterDetailData(clusterParam ,request);
			JSONObject o = Response.toJSONObject(res);
			ObjectMapper mapper = new ObjectMapper();
			r = mapper.readValue(o.toString(), Map.class);
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return r;
	}
	

	
	/**
	 * 查询 根据树状图条件聚合
	 * 
	 * @return
	 */
	@At("/clusterLandData")
	@GET
	@POST
	@Ok("json")
	public Object clusterLandData(@Param("clusterParam") String clusterParam ,HttpServletRequest request) {
		Response res = new Response();
		res.setSuccess(false);
		Object r = res;
		try {
			res = clusterService.clusterLandData(clusterParam ,request);
			JSONObject o = Response.toJSONObject(res);
			ObjectMapper mapper = new ObjectMapper();
			r = mapper.readValue(o.toString(), Map.class);
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return r;
	}
	
	/**
	 * 查询 根据项目进度条件聚合
	 * 
	 * @return
	 */
	@At("/clusterProjTypesData")
	@GET
	@POST
	@Ok("json")
	public Object clusterProjTypesData(@Param("projClusterPara") String[] projClusterPara ,HttpServletRequest request) {
		Response res = new Response();
		res.setSuccess(false);
		Object r = res;
		try {
			if(projClusterPara.length>0){
				res = clusterService.clusterProjTypesData(projClusterPara ,request);
				JSONObject o = Response.toJSONObject(res);
				ObjectMapper mapper = new ObjectMapper();
				r = mapper.readValue(o.toString(), Map.class);
			}
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return r;
	}
	
	
	/**
	 * 查询 项目状态聚合具体信息
	 * 
	 * @return
	 */
	@At("/clusterProjTypesDetailData")
	@GET
	@POST
	@Ok("json")
	public Object clusterProjTypesDetailData(@Param("clusterParam") String clusterParam ,HttpServletRequest request) {
		Response res = new Response();
		res.setSuccess(false);
		Object r = res;
		try {
			res = clusterService.clusterProjTypeDetailData(clusterParam ,request);
			JSONObject o = Response.toJSONObject(res);
			ObjectMapper mapper = new ObjectMapper();
			r = mapper.readValue(o.toString(), Map.class);
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return r;
	}
	
	
	
	/**
	 * 导出聚合后的数据Excel 未用到[由前端导出]
	 * 
	 * @return
	 */
	@At("/exportClusterLandData")
	@GET
	@POST
	@Ok("json")
	public Object exportClusterLandData(@Param("clusterParam") String clusterParam ,HttpServletRequest request) {
		Response res = new Response();
		res.setSuccess(false);
		Object r = res;
		try {
			res = clusterService.exportClusterLandData(clusterParam ,request);
			JSONObject o = Response.toJSONObject(res);
			ObjectMapper mapper = new ObjectMapper();
			r = mapper.readValue(o.toString(), Map.class);
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return r;
	}
}
